/*******************************************************************************
 * Copyright (c) 2009 Dop Sun.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Dop Sun - initial API and implementation
 ******************************************************************************/
package com.softao.ming.gde.internal.core.natures;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 * GDE Plugin file.
 */
public class GdePluginFile {
	/** The name of the file. */
	public static final String FILE_NAME = "gwt.plugin.xml";
	
	/**
	 * @param outputStream
	 * @throws IOException
	 */
	public static final void createInitFile(OutputStream outputStream) throws IOException {
		GdePluginFile file = new GdePluginFile();
		file.save(outputStream);
	}
	private boolean mHasGwtClientSource = true;
	private boolean mGwtClientSourceOnly = false;
	
	/**
	 * 
	 */
	public GdePluginFile() {
		reset();
	}
	
	/**
	 * @param inputStream
	 * @throws IOException
	 */
	public void load(InputStream inputStream) throws IOException {
		reset();
		
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		InputStreamReader reader = new InputStreamReader(inputStream);
		InputSource source = new InputSource(reader);
		try {
			DocumentBuilder documentBuilder = factory.newDocumentBuilder();
			Document document = documentBuilder.parse(source);
			read(document.getDocumentElement());
		} catch (ParserConfigurationException e) {
			throw new IOException("Can not create the parser.", e);
		} catch (SAXException e) {
			throw new IOException("Failed paring the document.", e);
		}
	}
	
	/**
	 * @param outputStream
	 * @throws IOException
	 */
	public void save(OutputStream outputStream) throws IOException {
		String xmlString = toXmlString();
		
		BufferedOutputStream stream = new BufferedOutputStream(outputStream);
		byte[] data = xmlString.getBytes("UTF-8");
		stream.write(data);
	}
	
	/**
	 * @return XML string.
	 */
	public String toXmlString() {
		StringBuilder sb = new StringBuilder();
		sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
		sb.append("<gde>\n");
		sb.append("    <source");
		sb.append(" hasGwtClient=\"" + mHasGwtClientSource + "\"");
		sb.append(" gwtClientOnly=\"" + mGwtClientSourceOnly + "\"");
		sb.append("/>\n");
		sb.append("</gde>");
		return sb.toString();
	}
	
	
	private void read(Element element) {
		NodeList childNodes = element.getChildNodes();
		for (int i = 0; i < childNodes.getLength(); i++) {
			Node node = childNodes.item(i);
			if (node instanceof Element && node.getNodeName().equals("source")) {
				Element child = (Element)node;
				if (child.hasAttribute("hasGwtClient")) {
					mHasGwtClientSource = "true".equals(child.getAttribute("hasGwtClient").toLowerCase());
				} else if (child.hasAttribute("gwtClientOnly")) {
					mGwtClientSourceOnly = "true".equals(child.getAttribute("gwtClientOnly").toLowerCase());
				}
			}
		}
	}
	
	private void reset() {
		mHasGwtClientSource = true;
		mGwtClientSourceOnly = false;
	}
}
